/**
 * Created with JetBrains PhpStorm.
 * User: vander
 * Date: 4/22/13
 * Time: 2:11 PM
 * To change this template use File | Settings | File Templates.
 */
var MyImage = Class.create(MyElement,{
    initialize: function($super,o){
        $super(o);
        this.className = "MyImage";

    },
    initAttributeList: function($super, o){
        $super(o);

        var _this = this;

        $j.each(o.element, function(){
            if(this.type == "image"){
                _this.attributeList.add(new MyAttribute(this));
                var imgTag = new Element("img");
                imgTag.setAttribute("src", this.value);
                imgTag.setStyle({width: "100%", height: "100%"});

                _this.divTag.insert(imgTag);
            }

        });
    },
    reloadAttributeList: function($super, o){
        $super(o);

        var _this = this;

        $j.each(o.element, function(){
            if(this.type == "image"){
                _this.attributeList.add(new MyAttribute(this));
                var imgTag = $j(_this.divTag).find("img:first");
                $j(imgTag).attr("src", this.value);
            }
        });
    },
    setValueByKey: function($super, key, value){
        if($super(key, value)){
            return true;
        }
        else{
            var att = this.attributeList.getAttributeFromKey(key);
            if(att !== null){
                var matched = false;

                if(att.type == "image"){
                    var imgTag = $(this.id).firstDescendant();
                    if(imgTag !== null){
                        $(imgTag).setAttribute("src", value);
                        matched = true;
                    }
                }
                if(matched){
                    this.attributeList.setValueByKey(key, value);
                    return true;
                }
            }
        }
        return false;
    },
    setValueByName: function($super, name, value){
        if($super(name, value)){
            return true;
        }
        else{
            var att = this.attributeList.getAttributeFromName(name);
            if(att !== null){
                var matched = false;
                if(att.type == "image"){
                    var imgTag = $(this.id).firstDescendant();
                    if(imgTag !== null){
                        $(imgTag).setAttribute("src", value);
                        matched = true;
                    }
                }
                if(matched){
                    this.attributeList.setValueByName(name, value);
                    return true;
                }
            }
        }
        return false;
    }
});